package com.tulele.vtool.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.tulele.vtool.mapper.AdvertInfoMapper;
import com.tulele.vtool.mapper.HeadlineMapper;
import com.tulele.vtool.model.*;
import com.tulele.vtool.service.AdvertInfoService;
import com.tulele.vtool.service.HeadlineService;
import com.tulele.vtool.utils.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class HeadlineServiceImpl implements HeadlineService {

    @Autowired
    private HeadlineMapper headlineMapper;

    @Override
    public List<Object> selectHeadimgUrlsByGzhCode(String gzhCode) {
        String imgUrls = this.headlineMapper.selectCarouselImgByGzhcode(gzhCode);
        if (StringUtils.isNotBlank(imgUrls)){
            return Arrays.stream(
                    imgUrls.split(","))
                    .map(String::trim)
                    .collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public Map<String, List<Object>> selectHeadlineByGzhCode(String gzhCode) {
        Map<String, List<Object>> map = Maps.newHashMap();
        // 查询该工作号下的轮播图
       String imgUrls = this.headlineMapper.selectCarouselImgByGzhcode(gzhCode);
       if (StringUtils.isNotBlank(imgUrls)){
           map.put("imgUrls", Arrays.stream(
                   imgUrls.split(","))
                   .map(String::trim)
                   .collect(Collectors.toList()));
       }
       //查询该公众号下的头条标题
        List<HeadlineTitleInfo> headlineTitleInfos = this.headlineMapper.selectHeadlineTitleByGzhcode(gzhCode);
       if (CollectionUtils.isNotEmpty(headlineTitleInfos)){
           map.put("headlineTitle",headlineTitleInfos.stream().map(url -> (Object) url).collect(Collectors.toList()));
       }
        return map;
    }

    @Override
    public List<ArticleInfo> selectArticleInfoByHeadlineTitleId(Integer pageNum, Integer pageSize, Integer headlineTitleId) {
        Integer num = this.headlineMapper.isVideo(headlineTitleId);
        if (num.equals(0)){
            // 查找文章
            PageHelper.startPage(pageNum,pageSize);
            return this.headlineMapper.selectArticleInfoByHeadlineTitleId(headlineTitleId);
        }else if (num.equals(1)){
            // 查找视频
            PageHelper.startPage(pageNum,pageSize);
            return this.headlineMapper.selectVideoByHeadlineTitleId(headlineTitleId);
        }
        return new ArrayList<>();
    }
}
